package org.example.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;

public class PdfPage8OldGenerator {
    public static void main(String[] args) throws Exception {
        // 1. 创建文档对象
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Page8.pdf"));

        // 2. 设置中文字体
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
        Font titleFont = new Font(bfChinese, 16, Font.BOLD, BaseColor.BLACK);
        Font headerFont = new Font(bfChinese, 12, Font.BOLD, BaseColor.DARK_GRAY);
        Font contentFont = new Font(bfChinese, 10, Font.NORMAL, BaseColor.BLACK);

        document.open();

        // 3. 添加问题汇总表
        PdfPTable mainTable = new PdfPTable(7);
        mainTable.setWidthPercentage(100);
        mainTable.setWidths(new float[]{1, 2, 4, 1.5f, 1.5f, 1.5f, 1.5f});

        // 表格标题
        addTableHeader(mainTable, "问题汇总表", 7, titleFont);

        // 列标题
        addTableCell(mainTable, "序号", headerFont);
        addTableCell(mainTable, "功能间", headerFont);
        addTableCell(mainTable, "问题项", headerFont);
        addTableCell(mainTable, "严重", headerFont);
        addTableCell(mainTable, "中等", headerFont);
        addTableCell(mainTable, "轻微", headerFont);
        addTableCell(mainTable, "其它", headerFont);

        // 填充数据（示例数据）
        int index = 1;
        while (index <= 56) {
            String currentArea = getFunctionalArea(index);
            int nextIndex = index + 1;

            // 找出连续相同功能间的最大索引
            while (nextIndex <= 56 && getFunctionalArea(nextIndex).equals(currentArea)) {
                nextIndex++;
            }

            int rowspan = nextIndex - index;

            // 添加第一行
            addTableCell(mainTable, String.valueOf(index), contentFont); // 序号
            PdfPCell areaCell = new PdfPCell(new Phrase(currentArea, contentFont));
            areaCell.setRowspan(rowspan); // 设置合并行数
            areaCell.setPadding(5);
            areaCell.setMinimumHeight(20);
            areaCell.setHorizontalAlignment(Element.ALIGN_CENTER); // 水平居中
            areaCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);  // 垂直居中

            mainTable.addCell(areaCell);
            addTableCell(mainTable, getIssueDescription(index), contentFont);
            mainTable.addCell(getSeverityMark(index, "严重", contentFont));
            mainTable.addCell(getSeverityMark(index, "中等", contentFont));
            mainTable.addCell(getSeverityMark(index, "轻微", contentFont));

            addTableCell(mainTable, "", contentFont);

            index++;

            // 添加后续行（只填充其他列，不重复添加功能间列）
            while (index < nextIndex) {
                addTableCell(mainTable, String.valueOf(index), contentFont); // 序号
                addTableCell(mainTable, getIssueDescription(index), contentFont);
                mainTable.addCell(getSeverityMark(index, "严重", contentFont));
                mainTable.addCell(getSeverityMark(index, "中等", contentFont));
                mainTable.addCell(getSeverityMark(index, "轻微", contentFont));

                addTableCell(mainTable, "", contentFont);
                index++;
            }
        }
        document.add(mainTable);

        // 4. 添加备注内容
        Paragraph remarks = new Paragraph("备注内容：", headerFont);
        remarks.add(new Paragraph("1) 室内灯具安装后建议连续通电试验8小时", contentFont));
        remarks.add(new Paragraph("2) 建议进行室内环境检测", contentFont));
        document.add(remarks);

        // 5. 添加签名区域
        PdfPTable signTable = new PdfPTable(1);
        signTable.setWidthPercentage(50);
        signTable.addCell(createSignatureCell("业主签名：___________________", contentFont));
        document.add(signTable);

        document.close();
        writer.close();
    }

    // 辅助方法：添加表头
    private static void addTableHeader(PdfPTable table, String text, int colspan, Font font) {
        PdfPCell cell = new PdfPCell(new Phrase(text, font));
        cell.setColspan(colspan);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(cell);
    }

    // 辅助方法：添加普通单元格
    private static void addTableCell(PdfPTable table, String text, Font font) {
        PdfPCell cell = new PdfPCell(new Phrase(text, font));
        cell.setPadding(5);
        cell.setMinimumHeight(20);
        table.addCell(cell);
    }

    // 辅助方法：创建签名单元格
    private static PdfPCell createSignatureCell(String text, Font font) {
        PdfPCell cell = new PdfPCell(new Phrase(text, font));
        cell.setBorder(Rectangle.TOP);
        cell.setPaddingTop(30);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        return cell;
    }

    // 示例数据生成方法（需根据实际数据替换）
    private static String getFunctionalArea(int index) {
        if(index <= 4) return "入户门";
        if(index <= 10) return "客餐厅";
        if(index <= 16) return "东北卧室";
        if(index <= 22) return "东南卧室";
        if(index <= 29) return "西南卧室";
        if(index <= 39) return "主卫";
        if(index <= 49) return "客卫";
        return "厨房/阳台";
    }

    private static String getIssueDescription(int index) {
        return "示例问题描述 " + index;
    }

    private static Phrase getSeverityMark(int index, String type, Font font) {
        String mark = "";
        BaseColor color = BaseColor.BLACK;

        if (type.equals("严重") && index % 10 == 0) {
            mark = "●";
            color = BaseColor.RED;
        } else if (type.equals("中等") && index % 5 == 0) {
            mark = "●";
            color = new BaseColor(255, 165, 0); // 橙色
        } else if (type.equals("轻微") && index % 3 == 0) {
            mark = "●";
            color = BaseColor.BLUE;
        }

        Font coloredFont = new Font(font.getBaseFont(), font.getSize(), font.getStyle(), color);
        return new Phrase(mark, coloredFont);
    }
}